/**
 * ReportGear(2011)
 */
package com.reportgear.core.data;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;

import net.abbp.core.util.NumberUtils;
import netscape.javascript.JSObject;

import com.reportgear.core.data.model.SalaryDataObject;
import net.abbp.core.util.StringUtils;
import net.abbp.report.utils.Dom4jUtils;

import com.reportgear.report.setting.AppletSettings;

/**
 * 通过Js查询薪酬数据,Applet使用本方式
 * 
 * @version 1.0 2011-5-28
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since Report 1.0
 */
public class SalaryDataQueryXmlByJs extends AbstractSalaryDataQueryXml {
    @Override
    public List<SalaryDataObject> getOrgs() {
        return this.xmlDataToList(this.query("GetOrgs.ashx", new Object[0]));
    }

    @Override
    public List<SalaryDataObject> getCenters() {
        return this.xmlDataToList(this.query("GetCenters.ashx", new Object[0]));
    }

    @Override
    public List<SalaryDataObject> getSalaryScheme() {
        return this.xmlDataToList(this.query("GetSalarySystems.ashx", new Object[0]));
    }

    @Override
    public List<SalaryDataObject> getSalaryItem(int schemeId) {
        return this.xmlDataToList(this.query("GetSalaryItemsBySysID.ashx", new Object[] { "ID", schemeId }));
    }

    @Override
    public String loadTemplate(int reportId) {
        return this.query("GetTemplateByID.ashx", new Object[] { "ID", reportId });
    }

    @Override
    public List<SalaryDataObject> loadTemplateList() {
        return this.xmlDataToList(this.query("GetTemplates.ashx", new Object[0]));
    }

    @Override
    public int saveTemplate(String template, int id, String name) {
        return NumberUtils.convertToInteger(this.xmlQuery("SaveTemplate.ashx?ID={0}&SystemID={1}&Name={2}", template,
                new Object[] { id, 0, name }), 0);
    }

    @Override
    public boolean deleteTemplate(int id) {
        return !StringUtils.isEmpty(this.query("DeleteTemplateByID.ashx", new Object[] { "ID", id }));
    }

    @Override
    public List<SalaryDataObject> getOrderInfo(String key) {
        return this.xmlDataToList(this.query("GetSalaryQuerySettingInfo.ashx", new Object[] { "key", key }));
    }

    @Override
    public int saveOrderInfo(String key, String content) {
        return NumberUtils.convertToInteger(this.xmlQuery("SaveSalaryQuerySettingInfo.ashx?key={0}", content,
                new Object[] { key }), 0);
    }

    @Override
    public List<SalaryDataObject> getLastPayments() {
        return this.xmlDataToList(this.query("GetSalaryPayInfos.ashx", new Object[0]));
    }

    @Override
    public SalaryDataObject getSessionVar() {
        return this.xmlRootToList(this.query("GetUserInfo.ashx", new Object[0]));
    }

    @Override
    public SalaryDataObject getPayBase(int payId) {
        String xml = this.query("GetSalaryPayInfo.ashx", new Object[] { "ID", payId });

        if (StringUtils.isEmpty(xml)) {
            return new SalaryDataObject();
        } else {
            Document doc = Dom4jUtils.createDocument(xml);
            Element root = doc.getRootElement();

            SalaryDataObject sdo = new SalaryDataObject();
            for (Object ao : root.attributes()) {
                Attribute aa = (Attribute) ao;

                sdo.setValue(aa.getName(), StringUtils.trim(aa.getValue()));
            }

            return sdo;
        }
    }

    /**
     * XML查询
     * 
     * @param command
     *            查询地址
     * @param queryXml
     *            xml文档
     * @param params
     *            参数
     * @return 查询返回结果
     */
    private String xmlQuery(String command, String queryXml, Object[] params) {
        String result = "";
        int paramCount = (params == null) ? 2 : params.length + 2;

        // 组装参数
        List<Object> paramList = new ArrayList<Object>();
        paramList.add(command);
        paramList.add(queryXml);
        for (Object o : params) {
            paramList.add(o);
        }

        JSObject jso = AppletSettings.INSTANCE.getJsObject();
        if (jso != null) {
            result = (String) jso.call("xmlQuery", paramList.toArray());
        }

        return result;
    }

    /**
     * 参数查询
     * 
     * @param command
     *            查询地址
     * @param params
     *            查询参数
     * @return 查询结果
     */
    private String query(String command, Object[] params) {
        String result = "";
        int paramCount = (params == null) ? 2 : params.length + 2;

        // 组装参数
        List<Object> paramList = new ArrayList<Object>();
        paramList.add(command);
        for (Object o : params) {
            paramList.add(o);
        }

        JSObject jso = AppletSettings.INSTANCE.getJsObject();
        if (jso != null) {
            result = (String) jso.call("query", paramList.toArray());
        }
        return result;
    }

    /**
     * XML 简历查询
     * 
     * @param command
     *            查询地址
     * @param queryXml
     *            xml文档
     * @param params
     *            参数
     * @return 查询返回结果
     */
    private String xmlResumeQuery(String command, String queryXml, Object[] params) {
        String result = "";
        int paramCount = (params == null) ? 2 : params.length + 2;

        // 组装参数
        List<Object> paramList = new ArrayList<Object>();
        paramList.add(command);
        paramList.add(queryXml);
        for (Object o : params) {
            paramList.add(o);
        }

        JSObject jso = AppletSettings.INSTANCE.getJsObject();
        if (jso != null) {
            result = (String) jso.call("xmlResumeQuery", paramList.toArray());
        }

        return result;
    }

    /**
     * 简历查询
     * 
     * @param command
     *            查询地址
     * @param params
     *            查询参数
     * @return 查询结果
     */
    private String queryResume(String command, Object[] params) {
        String result = "";
        int paramCount = (params == null) ? 2 : params.length + 2;

        // 组装参数
        List<Object> paramList = new ArrayList<Object>();
        paramList.add(command);
        for (Object o : params) {
            paramList.add(o);
        }

        JSObject jso = AppletSettings.INSTANCE.getJsObject();
        if (jso != null) {
            result = (String) jso.call("queryResume", paramList.toArray());
        }
        return result;
    }

    @Override
    public boolean deleteResumeTemplateModel(int id) {
        return !StringUtils.isEmpty(this.query("DeleteTemplateByID.ashx", new Object[] { "ID", id }));
    }

    @Override
    public String loadResumeTemplateModel(int id) {
        return this.queryResume("GetTemplateByID.ashx", new Object[] { "ID", id });
    }

    @Override
    public List<SalaryDataObject> loadResumeTemplateModelList() {
        return this.xmlDataToList(this.queryResume("GetTemplates.ashx", new Object[0]));
    }

    @Override
    public int saveResumeTemplateModel(String template, int id, String name, int isglobal) {
        return NumberUtils.convertToInteger(this.xmlResumeQuery(
                "SaveTemplate.ashx?ID={0}&SystemID={1}&Name={2}&isglobal={3}", template, new Object[] { id, 0, name,
                        isglobal }), 0);
    }

    @Override
    public SalaryDataObject getResumeBase(int id) {
        String xml = this.queryResume("GetResumeByPersonID.ashx", new Object[] { "ID", id });

        if (StringUtils.isEmpty(xml)) {
            return new SalaryDataObject();
        } else {
            Document doc = Dom4jUtils.createDocument(xml);
            Element root = doc.getRootElement();

            SalaryDataObject sdo = new SalaryDataObject();
            Element item = root.element("Item");
            if (item == null) {
                return new SalaryDataObject();
            } else {
                for (Object ao : item.attributes()) {
                    Attribute aa = (Attribute) ao;

                    sdo.setValue(aa.getName(), StringUtils.trim(aa.getValue()));
                }
            }

            return sdo;
        }
    }

    @Override
    public List<SalaryDataObject> getResumeParts(int id, String part) {
        return this
                .xmlDataToList(this.queryResume("GetResumeByPersonID.ashx", new Object[] { "ID", id, "Part", part }));
    }

    @Override
    public String getPhotoData(String photoId) {
        String xml = this.queryResume("GetPhoto.ashx", new Object[] { "ID", photoId });

        return xml;
    }

    @Override
    public List<SalaryDataObject> loadResumeTemplateModelList1() {
        return this.xmlDataToList(this.queryResume("GetTemplates.ashx?isglobal=1", new Object[0]));
    }
}
